package com.zoweunion.mechanic.service.impl;

import com.zoweunion.mechanic.dao.WirelessDeviceDao;
import com.zoweunion.mechanic.entity.TSBdCarDevRelationEntity;
import com.zoweunion.mechanic.service.WirelessDeviceService;
import com.zoweunion.mechanic.util.task.CarDataAbnormalTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

@Service
public class WirelessDeviceServiceImpl implements WirelessDeviceService {

    @Autowired
    private WirelessDeviceDao wirelessDeviceDao;

    @Autowired
    private CarDataAbnormalTask carDataAbnormalTask;

    @Override
    public void lightSenceWarningPushByDeviceId(String deviceId) {
        //1.根据deviceId 查询所有的金融平台管理员
        List<Map<String,Object>> managerList = wirelessDeviceDao.selectManagerListByDeviceId(deviceId);
        //2.根据deviceId 查询无线设备的相关信息
        Map<String,Object> deviceInfo = wirelessDeviceDao.selectDeviceInfoByDeviceId(deviceId);
        if(deviceInfo != null && deviceInfo.get("advice_name") != null) {
            //3.极光推送
            if (managerList.size() > 0) {
                managerList.forEach(item -> {
                    String userId = item.get("id").toString();
                    deviceInfo.put("s_id",item.get("s_id").toString());
                    carDataAbnormalTask.jPushLightSenceWarning(deviceInfo, userId, 1);
                });
            }
        } else {
            if (managerList.size() > 0) {
                managerList.forEach(item -> {
                    String userId = item.get("id").toString();
                    deviceInfo.put("s_id",item.get("s_id").toString());
                    carDataAbnormalTask.jPushLightSenceWarning(deviceInfo, userId, 2);
                });
            }
        }
    }

    @Override
    public PageInfo queryPage(Map<String, Object> map) {
//        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
//        Integer currentPage = Integer.valueOf(map.get("currentPage").toString());
        Integer pageSize = 3;
        Integer currentPage = 1;

        PageHelper.startPage(currentPage, pageSize);
        List<TSBdCarDevRelationEntity> mapList = wirelessDeviceDao.queryPage(map);
        PageInfo<TSBdCarDevRelationEntity> mapPageInfo = new PageInfo<>(mapList);
        return mapPageInfo;
    }

    @Override
    public void tSSsVehicleModelInsertUpdate(Map<String, Object> map) {
//        Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
//        Integer currentPage = Integer.valueOf(map.get("currentPage").toString());
        Integer pageSize = 3;
        Integer currentPage = 1;

        PageHelper.startPage(currentPage, pageSize);
        List<TSBdCarDevRelationEntity> mapList = wirelessDeviceDao.queryPage(map);

        // 更新
        if(mapList.size() > 0){
            System.out.println("更新");
            wirelessDeviceDao.updateDeviceNoByCarIdType(map.get("carId").toString(),map.get("type").toString(),map.get("devIdNo").toString());
        }else{
            // 新增
            wirelessDeviceDao.insertDeviceNo(map);
            System.out.println("新增");
        }

        PageInfo<TSBdCarDevRelationEntity> mapPageInfo = new PageInfo<>(mapList);
//        return mapPageInfo;
    }
}